add_library(galois_shmem)
add_library(Galois::shmem ALIAS galois_shmem)
set_target_properties(galois_shmem PROPERTIES EXPORT_NAME shmem)
add_dependencies(lib galois_shmem)

configure_file(src/Version.cpp.in Version.cpp @ONLY)
configure_file(include/galois/config.h.in include/galois/config.h)

set(sources
        "${CMAKE_CURRENT_BINARY_DIR}/Version.cpp"
        src/Barrier_Counting.cpp
        src/Barrier.cpp
        src/Barrier_Dissemination.cpp 
        src/Barrier_MCS.cpp
        src/Barrier_Pthread.cpp
        src/Barrier_Simple.cpp
        src/Barrier_Topo.cpp
        src/Context.cpp
        src/Deterministic.cpp
        src/DynamicBitset.cpp
        src/EnvCheck.cpp
        src/FileGraph.cpp
        src/FileGraphParallel.cpp
        src/gIO.cpp
        src/GraphHelpers.cpp
        src/HWTopo.cpp
        src/Mem.cpp
        src/NumaMem.cpp
        src/OCFileGraph.cpp
        src/PageAlloc.cpp
        src/PagePool.cpp
        src/PagePool.cpp
        src/ParaMeter.cpp
        src/PerThreadStorage.cpp
        src/PreAlloc.cpp
        src/Profile.cpp
        src/PtrLock.cpp
        src/SharedMem.cpp
        src/SharedMemSys.cpp
        src/SimpleLock.cpp
        src/Statistics.cpp
        src/Substrate.cpp
        src/Support.cpp
        src/Termination.cpp
        src/ThreadPool.cpp
        src/Threads.cpp
        src/ThreadTimer.cpp
        src/Timer.cpp
        src/Tracer.cpp
)

if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  list(APPEND sources src/HWTopoDarwin.cpp)
else()
  include(CheckSchedSetAffinity)
  if (NOT SCHED_SETAFFINITY_FOUND)
    if (GALOIS_STRICT_CONFIG)
      message(FATAL_ERROR "Need sched_setaffinity")
    endif()
  endif()
  list(APPEND sources src/HWTopoLinux.cpp)
endif()

target_sources(galois_shmem PRIVATE ${sources})

target_include_directories(galois_shmem PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
  $<INSTALL_INTERFACE:include>
)

if (TARGET Boost::Boost)
  # Autogenerated conan module doesn't provide header-only target. Extract one
  # manually.
  get_target_property(include_dirs Boost::Boost INTERFACE_INCLUDE_DIRECTORIES)
  target_include_directories(galois_shmem PUBLIC ${include_dirs})
else()
  # Standard CMake Boost module
  target_link_libraries(galois_shmem PUBLIC Boost::boost)
endif()

if (SCHED_SETAFFINITY_FOUND)
  target_compile_definitions(galois_shmem PRIVATE GALOIS_USE_SCHED_SETAFFINITY)
  target_link_libraries(galois_shmem PRIVATE ${SCHED_SETAFFINITY_LIBRARIES})
endif()

target_link_libraries(galois_shmem INTERFACE pygalois)
target_link_libraries(galois_shmem PRIVATE Threads::Threads)

if (CMAKE_HAVE_PTHREAD_H)
  target_compile_definitions(galois_shmem PRIVATE GALOIS_HAVE_PTHREAD)
endif()

find_package(NUMA)
if (NUMA_FOUND)
  target_compile_definitions(galois_shmem PRIVATE GALOIS_USE_NUMA)
  target_link_libraries(galois_shmem PRIVATE ${NUMA_LIBRARY})
else()
  message(WARNING "No NUMA Support.  Likely poor performance for multi-socket systems.")
endif()

if (VTune_FOUND)
  target_link_libraries(galois_shmem PRIVATE ${VTune_LIBRARIES})
endif()


add_subdirectory(test)

install(
  DIRECTORY include/
  DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
  COMPONENT dev
  FILES_MATCHING PATTERN "*.h"
)

install(
  DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
  DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
  COMPONENT dev
  FILES_MATCHING PATTERN "*.h"
)

install(
  TARGETS galois_shmem
  EXPORT GaloisTargets
  LIBRARY
    DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    COMPONENT shlib
  ARCHIVE
    DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    COMPONENT lib
  INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)
